package homework.homework_2;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class InputCont {
    private Lock lock = new ReentrantLock();
    private Condition c1 = lock.newCondition();
    private Condition c2 = lock.newCondition();
    private Condition c3 = lock.newCondition();
    private Condition c4 = lock.newCondition();
    private int num = 1;

    public void inputCont(String fileName, String content){
        try(
                OutputStream out = new FileOutputStream(new File(fileName), true)
        ){
            out.write(content.getBytes());
            out.flush();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public void aThread(String fileName, String content){
        try {
            lock.lock();
            while(num != 1){
                c1.await();
            }
            this.num = 2;
            Thread.sleep(1000);
            System.out.println(Thread.currentThread().getName()
                    + "向"
                    + fileName.substring(fileName.lastIndexOf(".") - 1)
                    + "写入文件 1");
            inputCont(fileName, content);
            c2.signal();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally{
            lock.unlock();
        }
    }

    public void bThread(String fileName, String content){
        try {
            lock.lock();
            while(num != 2){
                c2.await();
            }
            this.num = 3;
            Thread.sleep(1000);
            System.out.println(Thread.currentThread().getName()
                    + "向"
                    + fileName.substring(fileName.lastIndexOf(".") - 1)
                    + "写入文件 2");
            inputCont(fileName, content);
            c3.signal();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally{
            lock.unlock();
        }
    }

    public void cThread(String fileName, String content){
        try {
            lock.lock();
            while(num != 3){
                c3.await();
            }
            this.num = 4;
            Thread.sleep(1000);
            System.out.println(Thread.currentThread().getName()
                    + "向"
                    + fileName.substring(fileName.lastIndexOf(".") - 1)
                    + "写入文件 3");
            inputCont(fileName, content);
            c4.signal();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally{
            lock.unlock();
        }
    }

    public void dThread(String fileName, String content){
        try {
            lock.lock();
            while(num != 4){
                c4.await();
            }
            this.num = 1;
            Thread.sleep(1000);
            System.out.println(Thread.currentThread().getName()
                    + "向"
                    + fileName.substring(fileName.lastIndexOf(".") - 1)
                    + "写入文件 4");
            c1.signal();
            inputCont(fileName, content);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally{
            lock.unlock();
        }
    }
}
